<?php

namespace Home\Controller;

use Home\Core\CommandSDK;

class ClientController extends BaseController
{

    /**
     * 检索企业列表
     */
    public function client()
    {
        $param['name']=I('name'); //企业名称
        if(I('status') !== ''){   // 企业状态
            $param['status']=I('status');
        }
        if(empty(I('pageid'))){
            $param['pageid']=1;
        }else{
            $param['pageid']=I('pageid');
        }
        if(empty(I('pagecount'))){
            $param['pagecount']=10;
        }else{
            $param['pagecount']=I('pagecount');
        }
        $pageid=$param['pageid'];
        $pagecount=$param['pagecount'];
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::GetEnterpriseList, $param, 'GET');
        $totnum=$return->totnum;
        $pagenum=$totnum/$pagecount;
        $array = json_decode(json_encode($return), true);
        $this->assign('pageid', $pageid);//页数
        $this->assign('pagecount', $pagecount);//每页显示条数
        $this->assign('pagenum', $pagenum);//总页数
        $this->assign('totnum', $totnum); //总条数
        $this->assign('array', $array);
        $this->assign('param',$param);   // 搜索条件
        $this->display();
    }

    /**
     * 检索企业列表返回json
     */
    public function client_json()
    {
        $param = [
            'pageid'=>1,
            'pagecount'=>100000,
        ];
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::GetEnterpriseList, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }

    /**
     * 增加企业
     */
    public function addEnterprise_post()
    {
        $param = I('post.');
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::addEnterprise, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }

    /**
     * 修改企业启停状态
     */
    public function revStopEnterprise_post()
    {
        $param = I('post.');
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::revStopEnterprise, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);
    }


    /**
     * 检索客户列表
     */
  	public function client_xq()
    {
        $param = I('get.');
        if(empty($param['pageid'])){
            $param['pageid']=1;
        }else{
            $param['pageid']=$param['pageid'];
        }
        if(empty($param['pagecount'])){
            $param['pagecount']=10;
        }else{
            $param['pagecount']=$param['pagecount'];
        }
        $pageid=$param['pageid'];
        $pagecount=$param['pagecount'];
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::GetClientList, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $totnum=$return->totnum;
        $pagenum=$totnum/$pagecount;
        $this->assign('pageid', $pageid);//页数
        $this->assign('pagecount', $pagecount);//每页显示条数
        $this->assign('pagenum', $pagenum);//总页数
        $this->assign('totnum', $totnum); //总条数
        $this->assign('pagecount',$pagecount);
        $this->assign('getdata',$param);
        $this->assign('array', $array);
        $this->display();
    }

    /**
     * 增加客户.用户
     */
    public function addClient_post()
    {
        $param = I('post.');
        unset($param['cpasswd']);
        $passwd=$param['passwd'];
        $param['passwd']=substr(md5("$passwd"),0,16);
        $param['validDate'] = strtotime('+2 months');   // 临时加的字段，后期需要修改
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::AddUser, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);

    }

    /**
     * 修改客户.用户
     */
    public function modifyClient_post()
    {
        $param = I('post.');
        $param['validDate']=$param['validDate']*1;
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::ModifyUser, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);

    }

    /**
     * 删除客户.用户
     */
    public function delClient_post()
    {
        $param = I('post.');
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::DelUser, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);

    }

    /**
     * 检索设备列表
     */
    public function client_xq2()
    {
        $param = I('get.');
        if(empty($param['pageid'])){
            $param['pageid']=1;
        }else{
            $param['pageid']=$param['pageid'];
        }
        if(empty($param['pagecount'])){
            $param['pagecount']=10;
        }else{
            $param['pagecount']=$param['pagecount'];
        }
        $pageid=$param['pageid'];
        $pagecount=$param['pagecount'];
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::IntercomLists, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $totnum=$return->totnum;
        $pagenum=$totnum/$pagecount;
        $this->assign('pageid', $pageid);//页数
        $this->assign('pagecount', $pagecount);//每页显示条数
        $this->assign('pagenum', $pagenum);//总页数
        $this->assign('totnum', $totnum); //总条数
        $this->assign('param',$param); //搜索条件
        $this->assign('array', $array);
        $this->display();
    }
    /**
     * 检索订单列表
     */
    public function client_xq3()
    {
        $param = I('get.');
        if(empty($param['pageid'])){
            $param['pageid']=1;
        }else{
            $param['pageid']=$param['pageid'];
        }
        if(empty($param['pagecount'])){
            $param['pagecount']=10;
        }else{
            $param['pagecount']=$param['pagecount'];
        }
        $pageid=$param['pageid'];
        $pagecount=$param['pagecount'];
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::GetEnterprisesOpenList, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $totnum=$return->totnum;
        $pagenum=$totnum/$pagecount;
        $this->assign('pageid', $pageid);//页数
        $this->assign('pagecount', $pagecount);//每页显示条数
        $this->assign('pagenum', $pagenum);//总页数
        $this->assign('totnum', $totnum); //总条数
        $this->assign('param',$param); // 搜索条件
        $this->assign('array', $array);
        $this->display();
    }

    /**
     * 重置密码
     */
    public function ResetPswd_post()
    {
        $param = I('post.');
        $password=$param['passwd'];
        $param['passwd'] = substr(md5("$password"),0,16);
        $sdk = new CommandSDK();
        $return = $this->http_request($sdk::ResetPswd, $param, 'GET');
        $array = json_decode(json_encode($return), true);
        $array['msg']=$sdk::error_code[$return->result];
        $this->ajaxReturn($array);

    }


}